﻿using System;
using System.Web.Script.Serialization;

namespace Hxsoft
{
	public class Serialization
	{
		private static JavaScriptSerializer serializer;

		private static JavaScriptSerializer Serializer
		{
			get
			{
				if (serializer == null) serializer = new JavaScriptSerializer();
				return serializer;
			}
		}



		/// <summary>
		/// 将对象序列化为JSON字符串。
		/// </summary>
		/// <param name="obj">进行序列化的对象。</param>
		/// <returns>序列化后JSON字符串。</returns>
		public static string SerializeJson(object obj)
		{
			return Serializer.Serialize(obj);
		}

		/// <summary>
		/// 将JSON字符串反序列化为指定类型的对象。
		/// </summary>
		/// <typeparam name="T">指定的类型。</typeparam>
		/// <param name="json">进行反序列化的JSON字符串。</param>
		/// <returns>反序列化为指定类型的对象。</returns>
		public static T DeserializeJson<T>(string json)
		{
			return Serializer.Deserialize<T>(json);
		}
	}
}